//
//  YHCashAccountTabMenuView.m
//  YHApp
//
//  Created by -- on 2021/7/22.
//

#import "HACashAccountTabPopView.h"
#import "HAAccountModel.h"

@interface HACashAccountTabPopView ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;

@end

@implementation HACashAccountTabPopView

- (instancetype)initWithDicts:(NSArray *)array
{
    self = [super init];
    if (self) {
        [self.dataArray addObjectsFromArray:array];
        [self layoutViews];
    }
    return self;
}

#pragma mark - 点击事件
- (void)didTapBackgroundView:(UITapGestureRecognizer *)sender {
    [super didTapBackgroundView:sender];
}

-(void)cancelClick {
    [self dismiss];
}

-(void)show {
    [super show];
}
-(void)dismiss {
    [super dismiss];
}


-(void)layoutViews {
    
    self.frame = CGRectMake(0, 0, kUIScreen_w, kUIScreen_h);
    [self addSubview:self.backgroundView];
    
    //顶部视图
    CGFloat alertViewH = self.dataArray.count * 72 + 50 + 20 + app_bottomH;
    CGFloat alertViewW = kUIScreen_w;
    CGFloat alertViewX = 0;
    CGFloat alertViewY = kUIScreen_h-alertViewH;
    self.alertViewY = alertViewY;
    self.alertView.frame = CGRectMake(alertViewX, alertViewY+20, alertViewW, alertViewH);
    [self addSubview: self.alertView];
    self.alertView.backgroundColor = [UIColor whiteColor];
    self.alertView.layer.cornerRadius = 15;
    self.alertView.layer.masksToBounds = YES;

    UILabel *titLabel = [UILabel ha_NLabelWithFont:18 textColor:@"#111A34" text:@"提现账户" alignment:1];
    [self.alertView addSubview:titLabel];
    [titLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.centerX.mas_equalTo(0);
        make.height.mas_equalTo(50);
    }];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(titLabel.mas_bottom);
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.bottom.mas_equalTo(-app_bottomH);
    }];
    
    UIButton *cancelButton = [[UIButton alloc]init];
    [self.alertView addSubview:cancelButton];
    [cancelButton setImage:[UIImage imageNamed:@"hl_close01"] forState:UIControlStateNormal];
    [cancelButton addTarget:self action:@selector(cancelClick) forControlEvents:UIControlEventTouchUpInside];
    [self.alertView addSubview:cancelButton];
    [cancelButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.width.mas_offset(50);
        make.height.mas_offset(50);
    }];
    
}

#pragma mark - Table view data source
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 72;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *myCcell = [tableView dequeueReusableCellWithIdentifier:@"HAChooseTabPopViewCellID"];
    if (!myCcell) {
        myCcell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"HAChooseTabPopViewCellID"];
    }
    myCcell.selectionStyle = UITableViewCellSelectionStyleNone;
    HAAccountModel *model = self.dataArray[indexPath.row];
    BOOL isline = YES;
    if (indexPath.row == self.dataArray.count-1) {
        isline = NO;
    }
    [self setCellWith:myCcell.contentView model:model isLine:isline];
    return myCcell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self dismiss];
    if (self.YHCashAccountTabMenuSureBlock) {
        self.YHCashAccountTabMenuSureBlock(self.dataArray[indexPath.row]);
    }
}

-(void)setCellWith:(UIView *)contentView model:(HAAccountModel *)model isLine:(BOOL)isLine{
    for (UIView *view in contentView.subviews) {
        [view removeFromSuperview];
    }
    
    UIImageView *iconImgV = [UIImageView ha_createImageView];
    [iconImgV sd_setImageWithURL:[NSURL URLWithString:model.icon]];
    [contentView addSubview:iconImgV];
    [iconImgV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.width.mas_offset(38);
        make.height.mas_offset(38);
        make.centerY.mas_equalTo(0);
    }];
    
    UILabel *titLabel = [UILabel ha_NLabelWithFont:17 textColor:@"#111A34" text:model.text alignment:0];
    [contentView addSubview:titLabel];
    [titLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(iconImgV.mas_right).offset(10);
        make.right.mas_equalTo(-20);
        make.centerY.mas_equalTo(-12);
    }];
    
    UILabel *conLabel = [UILabel ha_NLabelWithFont:12 textColor:@"#858B9C" text:model.describe alignment:0];
    [contentView addSubview:conLabel];
    [conLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(iconImgV.mas_right).offset(10);
        make.right.mas_equalTo(-20);
        make.centerY.mas_equalTo(12);
    }];

    
    if (isLine) {
        UIView *lineView = [UIView ha_createView];
        lineView.backgroundColor = [UIColor khaBGColor];
        [contentView addSubview:lineView];
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.mas_equalTo(0);
            make.height.mas_offset(1);
        }];
    }
}
- (UITableView *)tableView {
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero];
        // 去掉滚动条
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self.alertView addSubview:_tableView];
    }
    return _tableView;
}

- (NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}


@end
